package com.example.administrator.waitan.mine;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.waitan.R;
import com.example.administrator.waitan.base.BaseActivity;
import com.example.administrator.waitan.find.art.ArtActivity;
import com.example.administrator.waitan.find.event.EventActivity;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by Administrator on 2016/1/19.
 */
public class BmiActivity extends BaseActivity{
    private Button countButton, btn;
    private EditText heighText;
    private RadioButton maleBtn, femaleBtn;
    String sex = "";
    double height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmi);

        //调用创建视图的函数
        creadView();
        //调用性别选择的函数
        sexChoose();
        findViewById(R.id.btn_bmi_goEvent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BmiActivity.this, EventActivity.class);
                Toast.makeText(BmiActivity.this, "想减肥---> 笑一会儿", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        });
        findViewById(R.id.btn_bmi_look).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == null){
                    AlertDialog.Builder builder = new AlertDialog.Builder(BmiActivity.this);
                    View view = LayoutInflater.from(BmiActivity.this).inflate(R.layout.dialog_bmi, null);
                    TextView show = (TextView) view.findViewById(R.id.tv_dialog_bmi);
                    show.setText("你是一位" + sexChoose() + "\n"
                            + "你的身高为" + Double.parseDouble(heighText.getText().toString()) + "cm"
                            + "\n你的标准体重为" + getWeight(sexChoose(), height) + "kg");
                    builder.setView(view);
                    builder.setPositiveButton("好吧", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            return;
                        }
                    });
                    builder.show();
                }
                else {
                    Toast.makeText(BmiActivity.this, "请输入身高和性别", Toast.LENGTH_SHORT).show();

                }
            }


        });
        findViewById(R.id.btn_bmi_goArt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BmiActivity.this, ArtActivity.class);
                Toast.makeText(BmiActivity.this, "想增肥---> 猥琐一会儿", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        });
        findViewById(R.id.btn_bmi_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    //性别选择的函数
    private String sexChoose() {
        if (maleBtn.isChecked()) {
            sex = "男性";
        } else if (femaleBtn.isChecked()) {
            sex = "女性";
        }
        return sex;
    }

    //创建视图的函数
    public void creadView() {

        countButton = (Button) findViewById(R.id.btn_bmi_goEvent);
        heighText = (EditText) findViewById(R.id.etx);
        maleBtn = (RadioButton) findViewById(R.id.male);
        femaleBtn = (RadioButton) findViewById(R.id.female);
    }

    //标准体重格式化输出的函数
    private String format(double num) {
        NumberFormat formatter = new DecimalFormat("0.00");
        String str = formatter.format(num);
        return str;
    }

    //得到标准体重的函数
    private String getWeight(String sex, double height) {
        height = Double.parseDouble(heighText.getText().toString());
        String weight = "";
        if (sex.equals("男性")) {
            weight = format((height - 80) * 0.7);
        } else {
            weight = format((height - 70) * 0.6);
        }
        return weight;
    }
}
